package com.ssm.controller.admin;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ssm.controller.SSMController;
import com.ssm.model.bean.SsmUserRules;
import com.ssm.model.bean.SsmUsers;
import com.ssm.model.mapper.ActionMapper;
import com.ssm.model.mapper.UserRuleMapper;


/**
 * SSM Controller动作类
 * 
 * @author WTC<546895488@qq.com>
 * @version 2016/1/29
 * */
@Controller
@RequestMapping(value="UserRule")
public class UserRuleController  extends AdminBaseController  {
	
	
	UserRuleMapper urm;
	/**
	 * 用户行为规则管理
	 * @param request
	 * @param response
	 */
	@RequestMapping(value="index")
	public String UserRule(HttpServletRequest request)
	{
		
		UserRuleMapper urm=M.getMapper(UserRuleMapper.class);
		urm.selectAll();
		List<SsmUserRules> list=urm.selectAll();
		this.List2Table(UserRuleMapper.class,null,request,false);	
		//this.analysisAction("action_index", request);
		//request.setAttribute("list", list);
		request.setAttribute("WEB_TITLE", "用户规则管理界面");
		return "UserRule/index";
	}

	/**
	 * 用户行为规则与日志管理修改
	 * @param request
	 * @param response
	 */
	@RequestMapping(value="edit")
	public String RuleEdit(HttpServletRequest request,SsmUserRules userrule,HttpSession httpSession)
	{
		urm=M.getMapper(UserRuleMapper.class);
		if(request.getMethod()=="POST")
		{
			if(userrule.getId()==null)//添加
			{
				//userrule.setCreatetime(this.nowTime().toString());
			    urm=M.getMapper(UserRuleMapper.class);
			    //设置默认用户规则状态
			    userrule.setStatus(S.getInteger("enrulestatus"));
			    userrule.setCreateuserid(1);//
			    urm.insert(userrule);
			    this.analysisAction("userrule_edit", request);
			    request.setAttribute("WEB_TITLE", "用户规则添加成功");
				return "User/login_ok";
			}
			else//修改
			{
				urm=M.getMapper(UserRuleMapper.class);
				urm.updateByPrimaryKeySelective(userrule);
				request.setAttribute("WEB_TITLE", "修改用户规则成功！");
				//SsmUsers user=(SsmUsers)httpSession.getAttribute("SsmUser");
				return "User/login_ok";
			}
			
		}
		else//编辑界面
		{
			Integer id=Integer.parseInt(request.getParameter("userruleid"));
			if(0==id)//添加信息
			{
				request.setAttribute("WEB_TITLE", "添加用户规则");
				userrule.setCreatetime(this.nowTime().toString());
				request.setAttribute("userrule",userrule);
				//this.analysisAction("userrule_edit", request);
				return "UserRule/edit";
			}
			else//修改信息
			{
				userrule=urm.selectByPrimaryKey(id);
				request.setAttribute("userrule",userrule);
				request.setAttribute("WEB_TITLE", "修改用户规则");
				//this.analysisAction("_edit", request);
				return "UserRule/edit";
			}
		}
	}
	
	@RequestMapping(value="changeStatus")
	public String changeStatus(HttpServletRequest request)
	{
		changeStatus(UserRuleMapper.class,request);
		return "redirect:index";
	}

}
